﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CVSA.Suporte.Sinacor.BMFBovespa.ConsultaRemedy.Common;
using CVSA.Suporte.Sinacor.BMFBovespa.Negocio;
using System.Web.UI.HtmlControls;

namespace CVSA.Suporte.Sinacor.BMFBovespa.ConsultaRemedy
{
    public partial class srmAtendimento : System.Web.UI.Page
    {
        private DataView odvChamados = new DataView();
        private AppCache appCache = new AppCache();
        private SRM srm = new SRM();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                ltUltimaAtualizacao.Text = srm.ultimoSincronismo().ToString();//.AddHours(1).ToString();
                //chkAreasChamado.DataSource = appCache.Tb_AreasChamado();
                //chkAreasChamado.DataTextField = "Area_Designada";
                //chkAreasChamado.DataValueField = "Area_Designada";
                //chkAreasChamado.DataBind();

                //foreach (ListItem lst in chkAreasChamado.Items)
                //{
                //    //if (lst.Text == "DO-CSSE SUPORTE SINACOR - ANALISE" || lst.Text == "DO-CSSE SUPORTE SINACOR - PRE-ANALISE")
                //    //{
                //    lst.Selected = true;
                //    //}
                //}
                if (!String.IsNullOrEmpty(Request.QueryString["IdIncidente"]))
                {

                    txtIdIncidente.Text = Request.QueryString["IdIncidente"];
                }
                if (!String.IsNullOrEmpty(Request.QueryString["Pessoa"]))
                {

                    txtPessoa.Text = Request.QueryString["Pessoa"];
                }

                btnPesquisar_Click(null, null);

            }
        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            try
            {
                //ltUltimaAtualizacao.Text = remedy.ultimoSincronismo().AddHours(1).ToString();

                if (rdTpPesquisaChamados.SelectedValue == "0")
                {
                    odvChamados.Table = appCache.Tb_ChamadosAbertosSRM();
                }


                string filter = string.Empty;
                filter = " 1 = 1 ";

                if (!String.IsNullOrEmpty(txtPessoa.Text))
                {
                    filter += " AND ASSIGNEE like  '%" + txtPessoa.Text + "%'";
                }

                filter += " AND Company Like '%" + txtNomeCorretora.Text.ToUpper() + "%'";
                if (!String.IsNullOrEmpty(txtIdIncidente.Text))
                {
                    filter += " And (Work_Order_id Like '%" + txtIdIncidente.Text + "%' OR SR_ID Like '%" + txtIdIncidente.Text + "%')";
                }

                string filterPrioridade = string.Empty;

                int i = 0;
                foreach (ListItem lst in chkLstPrioridade.Items)
                {
                    if (lst.Selected)
                    {
                        if (i == 0)
                        {
                            filterPrioridade += " AND (";
                            filterPrioridade += " Priority = '" + lst.Value + "'";
                        }
                        else
                        {
                            filterPrioridade += " OR Priority = '" + lst.Value + "'";
                        }
                        i++;
                    }
                }

                if (filterPrioridade != string.Empty)
                {
                    filterPrioridade += " )";
                    filter += filterPrioridade;
                }

                i = 0;

                string filterArea = string.Empty;


                //foreach (ListItem lst in chkAreasChamado.Items)
                //{
                //    if (lst.Selected)
                //    {
                //        if (i == 0)
                //        {
                //            filterArea += " AND (";
                //            filterArea += " AREA_DESIGNADA= '" + lst.Value + "'";
                //        }
                //        else
                //        {
                //            filterArea += " OR AREA_DESIGNADA = '" + lst.Value + "'";
                //        }
                //        i++;
                //    }
                //}

                if (filterArea != string.Empty)
                {
                    filterArea += " )";
                    filter += filterArea;
                }

                if (rdTpPesquisaChamados.SelectedValue == "1")
                {

                    if (txtNomeCorretora.Text.Length >= 3 || txtIdIncidente.Text.Length >= 6)
                    {
                        odvChamados.Table = appCache.Tb_ChamadosSRM(" WHERE " + filter);
                    }
                }
                else
                {
                    odvChamados.RowFilter = filter;
                }

                gvChamadosRemedy.DataSource = odvChamados;
                gvChamadosRemedy.DataBind();
                ltlQtdChamados.Text = odvChamados.Count.ToString();
            }
            catch (Exception ex)
            {
                //throw new Exception(ex.Message);
            }


        }

        public string FormatarTexto(string texto)
        {
            texto = texto.Replace("\n", "<br />");
            return texto;
        }

        protected void formatRow(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {

                DataRowView row = (DataRowView)e.Row.DataItem;
                HtmlAnchor href = (HtmlAnchor)e.Row.FindControl("showDetail");
                HtmlGenericControl div = (HtmlGenericControl)e.Row.FindControl("div_txt");

                //if (row["Area_Designada"].Equals("DO-CSSA SUPORTE SINACOR - NEGOCIO"))
                //{
                //    e.Row.Style.Add("background-color", "#990000");
                //    e.Row.Style.Add("color", "#FFFFFF");
                //    href.Style.Add("color", "#FFFFFF");
                //}

                if (row["Status"].Equals("In Progress"))
                {
                    e.Row.Style.Add("background-color", "#004080");
                    e.Row.Style.Add("color", "#FFFFFF");
                }

                if (row["Priority"].Equals("Critical"))
                {
                    e.Row.Style.Add("background-color", "#FFFFFF");
                    e.Row.Style.Add("color", "#990000");
                    e.Row.Style.Add("font-weight", "bold");
                }

                if (row["Status"].Equals("Closed") || row["Status"].Equals("Resolved") || row["Status"].Equals("Cancelled"))
                {
                    e.Row.Style.Add("background-color", "Teal");
                    e.Row.Style.Add("color", "#FFFFFF");
                }


                href.Attributes.Add("onclick", "javascript:showDetail(" + div.ClientID + ")");
                href.HRef = "#" + href.ClientID;

            }
        }
    }
}
